package com.cheche.dn.infrastructure;

import com.cheche.dn.Application;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.springframework.context.annotation.ComponentScan.Filter;

@Configuration
@EnableAspectJAutoProxy
@EnableScheduling
@ComponentScan(basePackageClasses = Application.class, excludeFilters = @Filter({Controller.class, RestController.class, Configuration.class}))
class ApplicationConfig implements SchedulingConfigurer {
	
	@Bean
	public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocations(
//				new ClassPathResource("/solr.properties"),
				new ClassPathResource("/config.properties"),
				new ClassPathResource("/redis.properties"),
				new ClassPathResource("/application.properties"),
				new ClassPathResource("/persistence.properties"));
		return ppc;
	}

	@Bean(name = "commonExecutorService", destroyMethod = "shutdown")
	public ExecutorService commonExecutorService() {
		return Executors.newScheduledThreadPool(10);
	}

	@Bean(name = "singleThreadExecutor", destroyMethod = "shutdown")
	public ExecutorService singleThreadExecutor() {
		return Executors.newSingleThreadExecutor();
	}


	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.setScheduler(commonExecutorService());
	}
}